CFLAGS		= -c -Os -std=c99 -m32 -Wall -Wshadow -W -Wconversion -Wno-sign-conversion  -fno-stack-protector -fomit-frame-pointer -fno-builtin -fno-common  -ffreestanding  -Wno-unused-parameter -Wunused-variable
ASMBFLAGS	= -f elf
OJCYFLAGS	= -S -O binary
#OJCYFLAGS	= -O binary
#LDFLAGS		= -s -static -T hello.lds -n -Map HelloOS.map
LDFLAGS2		= -s -static -T hello.lds
#LDFLAGS2		= -static -T hello.lds
#OJCYFLAGS	= -S -O binary

C_FILE := $(wildcard ./*.c)
C_FILE += $(wildcard ./*/*.c)
COBJ_FILE := $(patsubst %.c,%.o,$(C_FILE))

DRIVE_ASM_FILE := $(wildcard ./drive_asm/*.asm)
DRIVE_ASM_OBJ_FILE := $(patsubst %.asm,%.o,$(DRIVE_ASM_FILE))



bootstage1.bin: bootstage1.asm
	nasm -f bin $^ -o $@
bootstage2.o: bootstage2.asm
	nasm -f elf32 $^ -o $@
os.elf: bootstage2.o $(COBJ_FILE) $(DRIVE_ASM_OBJ_FILE)
	ld $(LDFLAGS2) $^ -o $@
os.bin: os.elf
	objcopy $(OJCYFLAGS) $< $@
%.o: %.c
	gcc $(CFLAGS) $^  -o $@
%.o: %.asm
	nasm -f elf32 $< -o $@

dd: bootstage1.bin os.bin
	dd if=./bootstage1.bin of=./complete.bin bs=512 count=1 seek=0
	dd if=./os.bin of=./complete.bin bs=512 count=2047 seek=1
img : dd
	qemu-img create -f qcow2 disk.img 1024k
	dd if=./complete.bin of=disk.img bs=512 count=2047
	dd if=/dev/zero of=disk.img bs=512 count=1 seek=2046 conv=notrunc


bochs: img
	bochs -q -f ./bochsrc
qemu: img
	qemu-system-i386 -drive format=raw,file=./disk.img
qemudebug : img
	qemu-system-i386 -hda ./disk.img -S -s
clean:
	rm *.elf -f
	rm *.o -f
	rm *.bin -f
	rm *.img -f;
	rm ./*/*.o;
	rm ./*/*.bin;
	rm ./*/*.img;
	rm ./*/*.elf;